package com.wanpinghui.supplier.oauth;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.object.HomePageActivity;
import com.wanpinghui.supplier.user.BusinessScopeActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import retrofit.callback.YRequestCallback;


@ContentView(R.layout.layout_activity_input_info)
public class InputInfoActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.tv_skip)
    private TextView textViewSkip;
    @ViewInject(R.id.iv_male)
    private ImageView mButtonMale;
    @ViewInject(R.id.iv_female)
    private ImageView mButtonFemale;
    @ViewInject(R.id.edt_name)
    private YunShlEditText mEditTextName;
    @ViewInject(R.id.edt_company_name)
    private YunShlEditText mEditTextCompany;
    @ViewInject(R.id.iv_company)
    private ImageView mButtonCompany;
    @ViewInject(R.id.iv_person)
    private ImageView mButtonPerson;
    @ViewInject(R.id.btn_next)
    private ThrottleButton mButtonNext;
    @ViewInject(R.id.company_line)
    private View mViewLine;
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayout mLayoutTitle;

    private boolean fromLogin;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            fromLogin = getIntent().getBooleanExtra("from_login", false);
        }
        if (fromLogin) {
            mLayoutTitle.setVisibility(View.GONE);
            textViewSkip.setVisibility(View.VISIBLE);
        } else {
            mLayoutTitle.setVisibility(View.VISIBLE);
            textViewSkip.setVisibility(View.GONE);
        }
    }

    @Override
    public void bindEvents() {
        mButtonMale.setOnClickListener(this);
        mButtonFemale.setOnClickListener(this);
        mButtonCompany.setOnClickListener(this);
        mEditTextName.setType(YunShlEditText.TYPE_ONLY_ZH);
        mEditTextCompany.setType(YunShlEditText.CATEGORY_CHARACTER);
        mButtonPerson.setOnClickListener(this);
        mButtonNext.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUserInfo();
            }
        });

        mEditTextName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkParams();
            }
        });

        mEditTextCompany.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkParams();
            }
        });

        textViewSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserBean userBean = WPHSdk.getService(OAuthService.class).getUserInfo(null);
                if (userBean.haveSerBusiness()) {
                    Intent intent = new Intent(InputInfoActivity.this, HomePageActivity.class);
                    intent.putExtra("check", true);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(InputInfoActivity.this, BusinessScopeActivity.class);
                    intent.putExtra("from_login", true);
                    startActivity(intent);
                }
            }
        });

        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void checkParams() {
        if (TextUtil.isEmpty(mEditTextName.getTextString()) || (mButtonCompany.isSelected() && TextUtil.isEmpty(mEditTextCompany.getTextString()))) {
            mButtonNext.setEnabled(false);
        } else {
            mButtonNext.setEnabled(true);
        }
    }

    private void updateUserInfo() {
        int gender = mButtonMale.isSelected() ? 1 : 0;
        WPHSdk.getService(OAuthService.class)
                .certifySupplier(mEditTextName.getTextString(), gender, null, mButtonCompany.isSelected() ? mEditTextCompany.getTextString() : null, null, new YRequestCallback<UserBean>() {
                    @Override
                    public void onSuccess(UserBean var1) {
                        if (var1.haveSerBusiness()) {
                            Intent intent = new Intent(InputInfoActivity.this, HomePageActivity.class);
                            intent.putExtra("check", true);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(InputInfoActivity.this, BusinessScopeActivity.class);
                            intent.putExtra("from_login", true);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });
    }

    @Override
    public void initData() {
        mButtonMale.setSelected(true);
        mButtonCompany.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_male:
                mButtonMale.setSelected(true);
                mButtonFemale.setSelected(false);
                break;
            case R.id.iv_female:
                mButtonMale.setSelected(false);
                mButtonFemale.setSelected(true);
                break;
            case R.id.iv_person:
                mButtonCompany.setSelected(false);
                mButtonPerson.setSelected(true);
                mEditTextCompany.setVisibility(View.GONE);
                mViewLine.setVisibility(View.GONE);
                checkParams();
                break;
            case R.id.iv_company:
                mButtonCompany.setSelected(true);
                mButtonPerson.setSelected(false);
                mEditTextCompany.setVisibility(View.VISIBLE);
                mViewLine.setVisibility(View.VISIBLE);
                checkParams();
                break;
        }
    }
}
